package cn.mooncode.basketballManage.controller;


import cn.mooncode.basketballManage.pojo.RespBean;
import cn.mooncode.basketballManage.pojo.User;
import cn.mooncode.basketballManage.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jwz
 * @since 2021-03-28
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "获取所有用户")
    @GetMapping("/")
    public List<User> getAllUsers(String keywords) {
        return userService.getAllUsers(keywords);
    }

    @ApiOperation(value = "获取当前用户信息")
    @GetMapping("/currentUser")
    public User getUser() {
        return (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }

    @ApiOperation(value = "删除用户信息")
    @DeleteMapping("/{id}")
    public RespBean deleteUser(@PathVariable Integer id) {
        return userService.deleteUser(id);
    }

    @ApiOperation(value = "修改用户信息")
    @PutMapping("/")
    public RespBean updateUser(@RequestBody User user) {
        if (userService.updateById(user)) {
            return RespBean.success("修改成功！");
        }
        return RespBean.error("修改失败！");
    }

    @ApiOperation(value = "修改用户角色")
    @PutMapping("/roles")
    public RespBean updateUserRole(@RequestBody Map<String, Integer> roles) {
        Integer id = roles.get("id");
        Integer rid = roles.get("rid");
        return userService.updateUserRole(id, rid);
    }

    @ApiOperation(value = "更新用户密码")
    @PutMapping("/updatePassword")
    public RespBean updateUserPassword(@RequestBody Map<String, Object> info) {
        String oldPass = (String) info.get("oldPass");
        String pass = (String) info.get("pass");
        Integer userid = (Integer) info.get("userid");
        return userService.updateUserPassword(oldPass, pass, userid);
    }
}
